package com.zhangyu.eat.login.view;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhangyu.eat.R;
import com.zhangyu.eat.base.BaseActivity;
import com.zhangyu.eat.bomain.FirstActivity;
import com.zhangyu.eat.login.presenter.Loginpresenter;
import com.zhangyu.eat.main.view.MainActivity;
import com.zhangyu.eat.register.view.RegisterActivity;
import com.zhangyu.eat.sql.UserDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity implements ILoginView {

    @BindView(R.id.toolbar_image)
    ImageView toolbarImage;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.login_toolbar)
    Toolbar loginToolbar;
    @BindView(R.id.login_name)
    EditText loginName;
    @BindView(R.id.login_password)
    EditText loginPassword;
    @BindView(R.id.login_button)
    Button loginButton;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    @BindView(R.id.toolbar_register)
    TextView toolbarRegister;
    private Loginpresenter presenter;
    //    private SQLiteDatabase mDb;
//    private UserDao mDao;
//    private Cursor cursor;
    private String name;
    private ArrayList<Map<String, Object>> mData;

    @Override
    protected void initView() {
//        mDao = new UserDao(this);
//        mDb = mDao.getDb();
        presenter = new Loginpresenter(this);
        mData = new ArrayList<>();
        OnClick();
    }

    private void OnClick() {
        toolbarRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.register();
            }
        });
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.login();
            }
        });
        toolbarImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public String getUserName() {
        name = loginName.getText().toString();
        return name;
    }

    @Override
    public String getPassWord() {
        String password = loginPassword.getText().toString();
        return password;
    }

    @Override
    public void resetMsg() {

    }

    @Override
    public void showLoginProgress() {

    }

    @Override
    public void hideLoginProgress() {

    }

    @Override
    public void toSucceedActivity() {
        ContentValues values = new ContentValues();
        String sql = "select * from user";
//        cursor = mDb.rawQuery(sql, null);
//        if (cursor != null) {
//            while (cursor.moveToNext()) {
//                HashMap<String, Object> map = new HashMap<>();
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                map.put("name", name);
//                mData.add(map);
//            }
//            //要记得关闭cursor
//            cursor.close();
//        }
//        long id = mDb.insert("user", null, values);
//        if (id > 0) {
//            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//        }
//        for (int i = 0; i < mData.size(); i++) {
//            Log.i("CCC",(String) mData.get(i).get("name")+mData.get(i).get("password"));
//            if(name.equals((String) mData.get(i).get("name"))){
//                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//
//            }else{
//                Toast.makeText(LoginActivity.this, "用户名密码错误", Toast.LENGTH_SHORT).show();
//            }
//
//        }
       finish();
        startActivity(new Intent(LoginActivity.this, FirstActivity.class));

    }

    @Override
    public void showLoginFailedView() {
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void toRegisterActivity() {
        startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
    }
}
